package com.wrb.pwd.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wrb
 * @date 2019/10/9 15:56
 */
public class PageUtils<T> {

	private int pageNo;
	private int pageSize;
	private int pages;
	private long count;
	private List<T> list;

	public static PageUtils getInstance() {
		return Inner.INSTANCE;
	}

	private static class Inner {
		private static final PageUtils INSTANCE = new PageUtils();
	}

	private PageUtils() {
	}

	public static PageUtils startPage(int pageNo, int pageSize, List list) {
		PageUtils page = PageUtils.getInstance();
		page.pageNo = pageNo;
		page.pageSize = pageSize;
		page.count = list.size();
		page.pages = (int) Math.ceil(page.count * 1.0 / pageSize * 1.0);
		if (page.pages == 0) {
			page.pages = 1;
		}
		if (pageNo + 1 > page.pages) {
			page.list = new ArrayList();
		} else if (pageNo + 1 == page.pages) {
			page.list = list.subList(pageNo * pageSize, list.size());
		} else {
			page.list = list.subList(pageNo * pageSize, pageNo * pageSize + pageSize);
		}
		return page;
	}

	public List<T> getList() {
		return list;
	}

	public int getPages() {
		return pages;
	}
}
